<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货品编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入货品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入货品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否限高" prop="beLimitHeight">
        <el-select v-model="queryParams.beLimitHeight"
                   clearable
                   style="width: 240px">
          <el-option 
          v-for="item in beLimitList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性" prop="attribute">
        <el-input
          v-model="queryParams.attribute"
          placeholder="请输入属性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="classAbc">
        <el-input
          v-model="queryParams.classAbc"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="scpName">
        <el-input
          v-model="queryParams.scpName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['core:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['core:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['core:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['core:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="itemId" />
      <el-table-column label="货品编码" align="center" prop="code" />
      <el-table-column label="货品名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="是否限高" align="center" prop="beLimitHeight">
        <template slot-scope="scope">
          {{ scope.row.beLimitHeight == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否翻包" align="center" prop="beScmTurn">
        <template slot-scope="scope">
          {{ scope.row.beScmTurn == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="容器类型" align="center" prop="mtType" />
      <el-table-column label="分类" align="center" prop="classAbc" />
      <el-table-column label="供应商编码" align="center" prop="scpCode" />
      <el-table-column label="供应商名称" align="center" prop="scpName" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['core:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['core:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="货品编码" prop="code">
               <el-input v-model="form.code" placeholder="请输入货品编码" style="width: 200px;"/>
             </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="货品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入货品名称" style="width: 200px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="容器类型" prop="mtType">
               <el-select v-model="form.mtType"
                   clearable
                   style="width: 200px">
                 <el-option 
                   v-for="item in mtTypeList"
                   :key="item.value"
                   :label="item.label"
                   :value="item.value">
                 </el-option>
               </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否限高" prop="beLimitHeight">
          <el-select v-model="form.beLimitHeight"
                   clearable
                   style="width: 200px">
          <el-option 
          v-for="item in beLimitList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否翻包" prop="beScmTurn">
          <el-select v-model="form.beScmTurn"
                   clearable
                   style="width: 200px">
          <el-option 
          v-for="item in beScmTurnList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
          </el-col>

        </el-row>

        <el-row>

          <el-col :span="8">
            <el-form-item label="分类" prop="classAbc">
          <el-input v-model="form.classAbc" placeholder="请输入分类" style="width: 200px;"/>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商编码" prop="scpCode">
          <el-input v-model="form.scpCode" placeholder="请输入供应商编码" style="width: 200px;"/>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称" prop="scpName">
          <el-input v-model="form.scpName" placeholder="请输入供应商名称" style="width: 200px;"/>
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem, changeItemStatus } from "@/api/core/item";

export default {
  dicts: ['sys_normal_disable'],
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beDelete: null,
        createdTime: null,
        creatorBy: null,
        basePackId: null,
        code: null,
        name: null,
        status: null,
        beLimitHeight: null,
        beScmTurn: null,
        mtType: null,
        attribute: null,
        classAbc: null,
        scpCode: null,
        scpName: null,
        userName: null,
        pickingPersonnelId: null,
        price: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      beLimitList:[{
        value: '0',
        label: '否'
      },{
        value: '1',
        label: '是'
      }],
      beScmTurnList:[{
        value: '0',
        label: '否'
      },{
        value: '1',
        label: '是'
      }],
      mtTypeList:[{
        value: 'PALLET',
        label: '大托盘'
      },{
        value: 'XPALLET',
        label: '小托盘'
      },{
        value: 'BOX',
        label: '大母箱'
      },{
        value: 'XBOX',
        label: '小母箱'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        beDelete: null,
        createdTime: null,
        creatorBy: null,
        updateTime: null,
        updateBy: null,
        basePackId: null,
        code: null,
        name: null,
        status: "0",
        beLimitHeight: null,
        beScmTurn: null,
        mtType: null,
        attribute: null,
        classAbc: null,
        scpCode: null,
        scpName: null,
        userName: null,
        pickingPersonnelId: null,
        price: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.itemId || this.ids
      getItem(itemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.itemId != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除删除货品编号为"' + itemIds + '"的数据项？').then(function() {
        return delItem(itemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('core/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"吗？').then(function() {
        return changeItemStatus(row.itemId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    }
  }
};
</script>
